package cn.qp.controller;

import cn.qp.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @author qipan
 * @date 2020/01/10 19:54
 */
@RestController
public class AsyncTestController {

    @Autowired
    private AsyncService asyncService;

    @RequestMapping("/async")
    public Object hello() throws InterruptedException {
        long start = System.currentTimeMillis();
        final Future<String> task1 = asyncService.doTask1();
        final Future<String> task2 = asyncService.doTask2();
        final Future<String> task3 = asyncService.doTask3();
        while (true) {
            if (task1.isDone() && task2.isDone() && task3.isDone()) {
                break;
            }
            Thread.sleep(1000);
        }
        long end = System.currentTimeMillis();
        return "全部执行完成!耗时" + (end - start) + "毫秒";
    }
}
